package com.haiyi.software.app;

import java.util.HashMap;
import java.util.Map;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.haiyi.software.app.basic.ActivityBasicModifyInformation;
import com.haiyi.software.entity.UserInfo;
import com.haiyi.software.tools.MyTools;

public class ActivityModifyInformation extends ActivityBasicModifyInformation
		implements OnClickListener {

	public static final int SHOW_SUB_ACTIVITY_USERNAME = 1;
	public static final int SHOW_SUB_ACTIVITY_USEX = 2;
	public static final int SHOW_SUB_ACTIVITY_UBODY = 3;
	public static final int SHOW_SUB_ACTIVITY_BIRTHDAY = 4;
	public static final int SHOW_SUB_ACTIVITY_JOB = 5;
	public static final int SHOW_SUB_ACTIVITY_INCOME = 6;

	private Button btn_modify_save;

	private Button btn_modify_username;

	private Button btn_modify_sex;

	private Button btn_modify_body;

	private Button btn_modify_birthday;

	private Button btn_modify_job;

	private Button btn_modify_income;

	private TextView textview_username, textview_usex, textview_ubody,
			textview_birthday, textview_ujob, textview_income;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO 自动生成的方法存根
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_modify_information);
		initComp();
		initData();
	}

	UserInfo userInfo;

	private void initData() {

		userInfo = MainApplication.getInstance().getMineUserInfo();
		this.textview_birthday.setText(userInfo.getUbirthday());
		this.textview_income.setText(userInfo.getUmoney());
		this.textview_ubody.setText(userInfo.getUbody());
		this.textview_ujob.setText(userInfo.getUwork());
		this.textview_username.setText(userInfo.getUsername());
		this.textview_usex.setText(MyTools.getInstance().getSexNameBySexID(
				userInfo.getUsex()));
		this.textview_usex.setTag(userInfo.getUsex());
	}

	private void initComp() {

		this.btn_modify_birthday = (Button) this
				.findViewById(R.id.btn_modify_birthday);
		this.btn_modify_body = (Button) this.findViewById(R.id.btn_modify_body);
		this.btn_modify_income = (Button) this
				.findViewById(R.id.btn_modify_income);
		this.btn_modify_job = (Button) this.findViewById(R.id.btn_modify_job);
		this.btn_modify_save = (Button) this.findViewById(R.id.btn_modify_save);
		this.btn_modify_sex = (Button) this.findViewById(R.id.btn_modify_sex);
		this.btn_modify_username = (Button) this
				.findViewById(R.id.btn_modify_username);

		this.textview_birthday = (TextView) this
				.findViewById(R.id.textview_birthday);
		this.textview_username = (TextView) this
				.findViewById(R.id.textview_username);
		this.textview_usex = (TextView) this.findViewById(R.id.textview_usex);
		this.textview_ubody = (TextView) this.findViewById(R.id.textview_ubody);
		this.textview_ujob = (TextView) this.findViewById(R.id.textview_ujob);
		this.textview_income = (TextView) this
				.findViewById(R.id.textview_income);

		this.btn_modify_birthday.setOnClickListener(this);
		this.btn_modify_body.setOnClickListener(this);
		this.btn_modify_income.setOnClickListener(this);
		this.btn_modify_save.setOnClickListener(this);
		this.btn_modify_job.setOnClickListener(this);
		this.btn_modify_username.setOnClickListener(this);
		this.btn_modify_sex.setOnClickListener(this);

	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_modify_birthday:
			Intent birthdayIntent = new Intent();

			birthdayIntent.setClass(ActivityModifyInformation.this,
					ActivityModifyBirthday.class);

			birthdayIntent.putExtra("birthday",
					this.textview_birthday.getText());

			startActivityForResult(birthdayIntent, SHOW_SUB_ACTIVITY_BIRTHDAY);
			break;
		case R.id.btn_modify_body:

			Intent ubodyIntent = new Intent();

			ubodyIntent.setClass(ActivityModifyInformation.this,
					ActivityModifyUbody.class);

			ubodyIntent.putExtra("ubody", this.textview_ubody.getText());

			startActivityForResult(ubodyIntent, SHOW_SUB_ACTIVITY_UBODY);

			break;
		case R.id.btn_modify_income:

			Intent incomeIntent = new Intent();

			incomeIntent.setClass(ActivityModifyInformation.this,
					ActivityModifyIncome.class);

			incomeIntent.putExtra("income", this.textview_income.getText());

			startActivityForResult(incomeIntent, SHOW_SUB_ACTIVITY_INCOME);

			break;
		case R.id.btn_modify_save:

			Map<String, String> args = new HashMap<String, String>();

			args.put("username", this.textview_username.getText().toString()
					.trim());

			args.put("usex", this.textview_usex.getTag().toString().trim());

			args.put("ubirthday", this.textview_birthday.getText().toString()
					.trim());

			args.put("ubody", this.textview_ubody.getText().toString().trim());

			args.put("umoney", this.textview_income.getText().toString().trim());

			args.put("uwork", this.textview_ujob.getText().toString().trim());

			args.put("uheight", MainApplication.getInstance().getMineUserInfo()
					.getUheight());

			args.put("uedu", MainApplication.getInstance().getMineUserInfo()
					.getUedu());

			requestNetworkData(args);

			break;
		case R.id.btn_modify_job:

			Intent jobIntent = new Intent();

			jobIntent.setClass(ActivityModifyInformation.this,
					ActivityModifyJob.class);

			jobIntent.putExtra("job", this.textview_ujob.getText());

			startActivityForResult(jobIntent, SHOW_SUB_ACTIVITY_JOB);

			break;
		case R.id.btn_modify_username:

			Intent usernameIntent = new Intent();

			usernameIntent.setClass(ActivityModifyInformation.this,
					ActivityModifyUsername.class);

			usernameIntent.putExtra("username",
					this.textview_username.getText());

			startActivityForResult(usernameIntent, SHOW_SUB_ACTIVITY_USERNAME);

			break;
		case R.id.btn_modify_sex:

			Intent usexIntent = new Intent();

			usexIntent.setClass(ActivityModifyInformation.this,
					ActivityModifySex.class);

			usexIntent.putExtra("usex", this.textview_usex.getTag().toString());

			startActivityForResult(usexIntent, SHOW_SUB_ACTIVITY_USEX);

			break;
		default:
			break;
		}
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {

		switch (resultCode) {
		case SHOW_SUB_ACTIVITY_USERNAME:
			textview_username.setText(data.getStringExtra("username"));
			break;
		case SHOW_SUB_ACTIVITY_USEX:
			this.textview_usex.setText(MyTools.getInstance().getSexNameBySexID(
					data.getStringExtra("usex")));
			this.textview_usex.setTag(data.getStringExtra("usex"));
			break;
		case SHOW_SUB_ACTIVITY_UBODY:
			this.textview_ubody.setText(data.getStringExtra("ubody"));
			break;
		case SHOW_SUB_ACTIVITY_BIRTHDAY:
			this.textview_birthday.setText(data.getStringExtra("birthday"));
			break;
		case SHOW_SUB_ACTIVITY_JOB:
			this.textview_ujob.setText(data.getStringExtra("job"));
			break;
		case SHOW_SUB_ACTIVITY_INCOME:
			this.textview_income.setText(data.getStringExtra("income"));
			break;
		}
	}

	@Override
	public void requestFinish(JsonObject jsonObjectReturn) {
		// TODO 自动生成的方法存根

		try {
			JsonObject jsObj = jsonObjectReturn;

			Toast.makeText(getApplicationContext(),
					jsObj.get("desc").getAsString(), Toast.LENGTH_LONG).show();

			setResult(ActivityMine.SHOW_SUB_ACTIVITY_MODIFYUSERINFO);

			ActivityModifyInformation.this.finish();
		} catch (Exception e) {
			Toast.makeText(getApplicationContext(), "修改失败", Toast.LENGTH_LONG)
					.show();
		}

	}

	@Override
	protected void requestError() {
		// TODO 自动生成的方法存根

	}

	// /**
	// *
	// * 处理日期控件的Handler
	// */
	//
	// Handler saleHandler = new Handler() {
	//
	// @Override
	// public void handleMessage(Message msg) {
	//
	// switch (msg.what) {
	//
	// case ActivityModifyInformation.SHOW_SUB_ACTIVITY_BIRTHDAY:
	//
	// showDialog(DATE_DIALOG_ID);
	//
	// break;
	//
	// }
	//
	// }
	//
	// };
	//
	// @Override
	// protected Dialog onCreateDialog(int id) {
	//
	// switch (id) {
	//
	// case DATE_DIALOG_ID:
	//
	// return new DatePickerDialog(this, mDateSetListener, mYear, mMonth,
	//
	// mDay);
	//
	// }
	//
	// return null;
	//
	// }
	//
	// @Override
	// protected void onPrepareDialog(int id, Dialog dialog) {
	//
	// switch (id) {
	//
	// case DATE_DIALOG_ID:
	//
	// ((DatePickerDialog) dialog).updateDate(mYear, mMonth, mDay);
	//
	// break;
	//
	// }
	//
	// }
	//
	// /**
	// *
	// * 日期控件的事件
	// */
	//
	// private DatePickerDialog.OnDateSetListener mDateSetListener = new
	// DatePickerDialog.OnDateSetListener() {
	//
	// public void onDateSet(DatePicker view, int year, int monthOfYear,
	//
	// int dayOfMonth) {
	//
	// mYear = year;
	//
	// mMonth = monthOfYear;
	//
	// mDay = dayOfMonth;
	//
	// updateDisplay();
	//
	// }
	//
	// };
	//
	// /**
	// *
	// * 更新日期
	// */
	//
	// private void updateDisplay() {
	//
	// this.textview_birthday.setText(new StringBuilder().append(mYear)
	// .append(
	//
	// (mMonth + 1) < 10 ? "0" + (mMonth + 1) : (mMonth + 1)).append(
	//
	// (mDay < 10) ? "0" + mDay : mDay));
	//
	// }
}
